package com.briup.jd2407cms.service.impl;

import com.briup.jd2407cms.domain.DTO.LoginParam;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.service.LoginService;
import com.briup.jd2407cms.service.UserService;
import com.briup.jd2407cms.util.JwtUtil;
import com.briup.jd2407cms.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: JD2407-cms
 * @BelongsPackage: com.briup.jd2407cms.service.impl
 * @Author: 鲨鲨天下第一
 * @CreateTime: 2024-11-20  15:18
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String login(LoginParam loginParam) {
        //对密码加密与数据库进行比对
        User user = userService.prelogin(loginParam.getUsername(),
                MD5Utils.MD5(loginParam.getPassword()));
//                param.getPassword());
//        System.out.println("user = " + user);
        //新增token 相关代码
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user.getId());
        map.put("username", user.getUsername());
        // 放入isVip不合适，后期充值成功后，用户不会重新登录，token值不变，导致isVip值是错误的
//        map.put("isVip", user.getIsVip());
        map.put("roleId", user.getRoleId());
        return JwtUtil.generateJwt(map);
    }
}
